<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 
 * @param 
 *         $xxx = 作用
 * @static 
 *         XXX = 作用
 * 
 * 
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');
include(SERVER_ROOT_PATH.'/include/config/admin/ml_admin_queuename.php');

class ml_mq_fakedata_addgoods extends MqClass{
    const QUEUE_NAME = ML_ADMQUEUENAME_ADDFAKEDATA;
    private $oAdmin;
    private $fakeuser;

    public function _construct()
    {
        $this->oAdmin = new ml_model_admin_dbCommon();
        $this->fakeuser = ml_factory::load_standard_conf('fakedata_users');
    }

    /**
     * 注释
     *
     * @param array $message_data
     * array(
     *                        所接收的参数说明
     *        )
     * )
     */
    public function run_job(){
        //接收的数据
        $arr = $this->src_data;
        $fakeuser = $this->fakeuser;
        $this->oAdmin->autofetch_getbyid($arr['id']);
        $work = $this->oAdmin->get_data();

        $url = $work['url'];
        $iid = $work['num_iid'];
        $tag = $work['tag'];
        $class = $work['class'];

        if(!empty($tag) && $fakeuser['tag_biguser'][$tag])
            $users = $fakeuser['tag_biguser']['tag'];
        else if(!empty($class) && $fakeuser['class_biguser'][$class])
            $users = $fakeuser['tag_biguser']['tag'];
        else 
            $users = $fakeuser['smalluser'];

        //随机决定以哪个用户身份发
        shuffle($users);
        $rnd = mt_rand(0 , count($users)-1);
        $uid = $users[$rnd];
        $uid = 10000;


        $goods_url = 'http://item.taobao.com/item.htm?id='.$iid;
        
        $rs = Tool_http::post('http://'.ML_CNF_DOMAIN.'/fetch/fetch_api.php' , array('url' => $goods_url));
        var_dump($rs);
        $rs = json_decode($rs,1);
        if(!$rs || $rs['code']!=ML_RCODE_SUCC)
        {
            $this->oAdmin->autofetch_pass($work['id'] , 2);
            Tool_logger::debugLog(__FILE__ , 'fetch_err '.$goods_url);
            return false;
        }

        

        $data = $rs['data'];
        $rs = $this->_fk_publish($uid , 'xxxx' , $data);
        
        if($rs)
        {
            $this->oAdmin->autofetch_pass($work['id']);
            
            return true;

        }
            
            return true;
    }

    private function _fk_publish($uid , $content , $data)
{


    $publish_url = 'http://'.ML_CNF_DOMAIN.'/intraapi/aj_publish.php';

    $data['uid'] = $uid;
    $data['content'] = $content;//str_replace($find, $replace, str_replace('@', ' ',stripslashes($gurl['gd_title'])));
    $data['gd_pic'] = $data['gd_pic'];
    $data['gd_title'] = $data['gd_title'];
    $data['gd_url'] = $data['gd_url'];
    $data['gd_price'] = $data['gd_price'];
    $ecode = trim('!'.$tag[$data['type']].'###'.trim(str_replace(',,',',',$data['tag']),','),'!###');
    $data['ecode'] = $ecode;




    $result = Tool_http::post($publish_url, $data);
    
    $result = json_decode($result,true);
    
    if($result['code'] == ML_RCODE_SUCC)
        return true;
    else
        return false;
}

}

$conf_array = ml_factory::load_standard_conf('redis');
$conf_array = $conf_array['meila_queue'];
$xblog_obj = new ml_mq_fakedata_addgoods(new RsQueue(ml_mq_fakedata_addgoods::QUEUE_NAME , $conf_array));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();
